//
//  SCPreviewBottomView.m
//  PhotoAlbum
//
//  Created by Chao Sheng on 2021/12/7.
//

#import "SCPreviewBottomView.h"

@interface SCPreviewBottomCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, assign) BOOL isSelected;

@property (nonatomic, strong) SCPhotoModel *photoModel;

@end

@implementation SCPreviewBottomCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self=[super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (_isSelected) {
        self.imageView.layer.borderColor = sc_Blue.CGColor;
    } else {
        self.imageView.layer.borderColor = [UIColor clearColor].CGColor;
    }
}

- (void)setPhotoModel:(SCPhotoModel *)photoModel {
    _photoModel = photoModel;
    if (self.photoModel.tempImage) {
        self.imageView.image = self.photoModel.tempImage;
    } else {
        sc_weak(self);
        CGSize targetSize = CGSizeMake(self.frame.size.width*1.7, self.frame.size.height*1.7);
        self.imageView.image = nil;
        [SCPhotoTools getPhotoForPHAsset:_photoModel.asset size:targetSize completion:^(UIImage * _Nonnull image,PHAsset *asset) {
            if (asset==self.photoModel.asset) {
                weakself.imageView.image = image;
            }
        }];
    }
    
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.layer.borderWidth = 1.5;
        _imageView.layer.borderColor = [UIColor clearColor].CGColor;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
    }
    return _imageView;
}

@end

@interface SCPreviewBottomView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIToolbar *toolbar;

@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UIButton *sureBtn;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation SCPreviewBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.toolbar];
    [self addSubview:self.bottomLine];
    [self addSubview:self.sureBtn];
    [self addSubview:self.collectionView];
    
    [self.toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-15);
        make.centerY.equalTo(self).offset(-sc_BOTTOMPADDING_HEIGHT/2);
        make.width.equalTo(@50);
        make.height.equalTo(@30);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.equalTo(self.sureBtn.mas_left).offset(-15);
        make.height.equalTo(@40);
        make.top.equalTo(@5);
    }];
}

- (void)setSelectedArray:(NSArray<SCPhotoModel *> *)selectedArray {
    _selectedArray = selectedArray;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    [self.collectionView reloadData];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex-1 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

- (void)setHiddenCompletion:(BOOL)hiddenCompletion {
    _hiddenCompletion = hiddenCompletion;
    if (_hiddenCompletion==true) {
        self.sureBtn.hidden = true;
    } else {
        self.sureBtn.hidden = false;
    }
}

#pragma mark - action
- (void)sureAction {
    if (self.scCompletion) {
        self.scCompletion();
    }
}

#pragma mark - collectionView delegate & datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.selectedArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SCPreviewBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCPreviewBottomCell" forIndexPath:indexPath];
    cell.photoModel = self.selectedArray[indexPath.row];
    if (_selectedIndex>0) {
        if (indexPath.row+1==_selectedIndex) {
            cell.isSelected = true;
        } else {
            cell.isSelected = false;
        }
        
    } else {
        cell.isSelected = false;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndex = indexPath.row+1;
    
    if(self.scSrollPreview) {
        self.scSrollPreview(self.selectedArray[indexPath.row]);
    }
}

#pragma mark - 懒加载
- (UIToolbar *)toolbar {
    if (!_toolbar) {
        _toolbar = [[UIToolbar alloc] init];
        _toolbar.translucent = true;
        _toolbar.clipsToBounds = true;
    }
    return  _toolbar;
}

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    }
    return _bottomLine;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor.whiteColor colorWithAlphaComponent:0.5] forState:UIControlStateDisabled];
        _sureBtn.backgroundColor = sc_Blue;
        _sureBtn.layer.cornerRadius = 3;
        _sureBtn.layer.masksToBounds = true;
        _sureBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(30, 40);
        flowLayout.minimumInteritemSpacing = 3.5;
        flowLayout.minimumLineSpacing = 3.5;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        if (@available(iOS 11, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [_collectionView registerClass:[SCPreviewBottomCell class] forCellWithReuseIdentifier:@"SCPreviewBottomCell"];
    }
    return _collectionView;
}

@end
